package co.yixiang.modules.system.serviceimpl;

import cn.hutool.core.bean.BeanUtil;
import co.yixiang.cache.Cache;
import co.yixiang.cache.CachePrefix;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.modules.system.entity.domain.FreightTemplate;
import co.yixiang.modules.system.entity.domain.FreightTemplateChild;
import co.yixiang.modules.system.entity.dto.FreightTemplateQueryCriteria;
import co.yixiang.modules.system.entity.vo.FreightTemplateVo;
import co.yixiang.modules.system.mapper.FreightTemplateMapper;
import co.yixiang.modules.system.service.FreightTemplateChildService;
import co.yixiang.modules.system.service.FreightTemplateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "dict")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FreightTemplateServiceImpl extends BaseServiceImpl<FreightTemplateMapper, FreightTemplate> implements FreightTemplateService {
    private final static String CACHEKEY = CachePrefix.SHIP_TEMPLATE.getPrefix() + "123456";
    /**
     * 配送子模板
     */
    @Autowired
    private FreightTemplateChildService freightTemplateChildService;
    /**
     * 缓存
     */
    @Autowired
    private Cache cache;

    @Override
    public Map<String, Object> queryAll(FreightTemplateQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<FreightTemplate> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", page.getList());
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<FreightTemplate> queryAll(FreightTemplateQueryCriteria criteria) {
        final QueryWrapper predicate = QueryHelpPlus.getPredicate(FreightTemplate.class, criteria);
        predicate.eq("is_del", 0);
        return baseMapper.selectList(QueryHelpPlus.getPredicate(FreightTemplate.class, criteria));
    }

    @Override
    public List<FreightTemplateVo> getFreightTemplateList(String freightTemplateId) {
        //先从缓存中获取运费模板，如果有则直接返回，如果没有则查询数据后再返回
        List<FreightTemplateVo> list = (List<FreightTemplateVo>) cache.get(CACHEKEY);
        if (list != null) {
            return list;
        }
        list = new ArrayList<>();
        //查询运费模板
        LambdaQueryWrapper<FreightTemplate> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(FreightTemplate::getIsDel, 0);
        List<FreightTemplate> freightTemplates = this.baseMapper.selectList(lambdaQueryWrapper);
        if (!freightTemplates.isEmpty()) {
            //如果模板不为空则查询子模板信息
            for (FreightTemplate freightTemplate : freightTemplates) {
                FreightTemplateVo freightTemplateVO = new FreightTemplateVo();
                BeanUtil.copyProperties(freightTemplate, freightTemplateVO);
                List<FreightTemplateChild> freightTemplateChildren = freightTemplateChildService.getFreightTemplateChild(freightTemplate.getId());
                if (!freightTemplateChildren.isEmpty()) {
                    freightTemplateVO.setFreightTemplateChildList(freightTemplateChildren);
                }
                list.add(freightTemplateVO);
            }
        }
        cache.put(CACHEKEY, list);
        return list;

    }



    @Override
    public FreightTemplateVo getFreightTemplate(String id) {
        FreightTemplateVo freightTemplateVO = new FreightTemplateVo();
        //获取运费模板
        FreightTemplate freightTemplate = this.getById(id);
        if (freightTemplate != null) {
            //复制属性
            BeanUtils.copyProperties(freightTemplate, freightTemplateVO);
            //填写运费模板子内容
            List<FreightTemplateChild> freightTemplateChildList = freightTemplateChildService.getFreightTemplateChild(id);
            freightTemplateVO.setFreightTemplateChildList(freightTemplateChildList);
        }
        return freightTemplateVO;
    }

    @Override
    public FreightTemplateVo addFreightTemplate(FreightTemplateVo freightTemplateVO) {

        FreightTemplate freightTemplate = new FreightTemplate();

        //复制属性
        BeanUtils.copyProperties(freightTemplateVO, freightTemplate);
        //添加运费模板
        this.save(freightTemplate);
        //给子模板赋父模板的id
        List<FreightTemplateChild> list = new ArrayList<>();
        //如果子运费模板不为空则进行新增
        if (freightTemplateVO.getFreightTemplateChildList() != null) {
            for (FreightTemplateChild freightTemplateChild : freightTemplateVO.getFreightTemplateChildList()) {
                freightTemplateChild.setFreightTemplateId(freightTemplate.getId());
                list.add(freightTemplateChild);
            }
            //添加运费模板子内容
            freightTemplateChildService.addFreightTemplateChild(list);
        }

        //更新缓存
        cache.remove(CACHEKEY);
        return freightTemplateVO;
    }

    @Override
    public FreightTemplateVo editFreightTemplate(FreightTemplateVo freightTemplateVo) {
        FreightTemplate freightTemplate = new FreightTemplate();
        //复制属性
        BeanUtils.copyProperties(freightTemplateVo, freightTemplate);
        //修改运费模板
        this.updateById(freightTemplate);
        //删除模板子内容
        freightTemplateChildService.removeFreightTemplatePhysical(freightTemplateVo.getId());
        //给子模板赋父模板的id
        List<FreightTemplateChild> list = new ArrayList<>();
        for (FreightTemplateChild freightTemplateChild : freightTemplateVo.getFreightTemplateChildList()) {
            freightTemplateChild.setFreightTemplateId(freightTemplate.getId());
            list.add(freightTemplateChild);
        }
        //添加模板子内容
        freightTemplateChildService.addFreightTemplateChild(list);
        //更新缓存
        cache.remove(CACHEKEY);
        return null;
    }


    @Override
    public boolean removeFreightTemplate(String id) {
        //如果删除成功则删除运费模板子项
        if (this.removeById(id)) {
            cache.remove(CACHEKEY);
            return freightTemplateChildService.removeFreightTemplate(id);
        }
        return false;
    }
}
